// pages/writeReturnLogistics/writeReturnLogistics.js
var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 物流公司选择
    deliveryList:[],
    expressId: 0,  //物流公司id

    expressName: '',  //物流公司名称
    expressNo: '',  //物流单号
    imgs: [],  //图片凭证
    mobile: '', //手机号码
    refundSn: '',  //退款编号名称
    senderRemarks: '', //备注信息
  },

  /**
   * 选择物流公司
   */
  chooseLogisticsCompany: function (e) {
    console.log('快递公司',e.detail.value)
    this.setData({
      expressId: e.detail.value
    })
  },

  onExpressNoInput:function(e){
    this.setData({
      expressNo:e.detail.value
    });
  },

  onSenderRemarksInput: function (e) {
    this.setData({
      senderRemarks: e.detail.value
    });
  },


  /**
   * 填写物流信息
   */
  writeLogisticsMsg: function (e) {
    wx.showLoading();
    if (this.data.expressNo.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '请填写物流单号',
      })
    } else {
      var pics = '';
      this.data.imgs.forEach(function (item) {
        pics += item.path + ',';
      });
      if (pics != '') {
        pics = pics.substring(0, pics.length - 1)
      }
      var parmas = {
        url: "/p/orderRefund/submitExpress",
        method: "POST",
        data: {
          expressId: this.data.deliveryList[this.data.expressId].dvyId,  //物流公司id
          expressName: this.data.deliveryList[this.data.expressId].dvyName,  //物流公司名称
          expressNo: this.data.expressNo,  //物流单号
          imgs: pics,  //图片凭证
          mobile: this.data.mobile, //手机号码
          refundSn: this.data.refundSn,  //退款编号名称
          senderRemarks: this.data.senderRemarks, //备注信息
        },
        callBack: (res) => {
          wx.hideLoading();
          wx.navigateTo({
            url: '/pages/DetailsOfRefund/DetailsOfRefund?refundSn=' + this.data.refundSn,
          })
        }
      };
    http.request(parmas);
    }
  },



  /**
   * 上传图片
   */
  getUploadImg: function (e) {
    const idx = e.target.dataset.idx
    console.log(idx);
    var ths = this;
    wx.chooseImage({
      count: 5, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        // 图片的本地临时文件路径列表
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          mask: true
        })
        var params = {
          url: "/p/file/upload",
          filePath: tempFilePaths[0],
          name: 'file',
          callBack: function (res2) {
            wx.hideLoading();
            var img = {};
            img.path = JSON.parse(res2).filePath;
            img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
            var imgs = ths.data.imgs;
            imgs.push(img);
            ths.setData({
              imgs: imgs
            })
          }
        };
        http.upload(params);
      }
    })
  },

  /**
   * 删除图片
   */
  removeImage: function (e) {
    // const index = e.target.dataset.index
    // const idx = e.target.dataset.idx;
    var idx = e.currentTarget.dataset.idx;
    var imgs = this.data.imgs;
    imgs.splice(idx, 1)
    this.setData({
      imgs: imgs
    });
    console.log('删除图片')
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      refundSn: options.refundSn
    });
    this.loadDeliveryData();
  },

  loadDeliveryData(){
    wx.showLoading();
    http.request({
      url:"/p/delivery/list",
      method: "get",
      data: {},
      callBack: (res) => {
        wx.hideLoading();
        this.setData({
          deliveryList:res
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})